<?php

namespace App\Kuafu\Category\Model;

use App\Kuafu\Base\Model\Traits\KfSoftDeletes;
use App\Kuafu\Base\Model\Traits\KfUid;
use App\Kuafu\Category\Enum\CategoryCodeEnum;
use App\Kuafu\Category\Exceptions\CategoryException;
use App\Kuafu\Config\Service\ConfigService;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Kuafu\Base\Model\BaseModel;

class CategoryModel extends BaseModel
{
    use HasFactory, KfSoftDeletes, KfUid;

    protected $fillable = [
        'brandId',
        'name',
        'desc',
        'imageUrl',
        'sort',
        'menuId',
        'pid',
    ];

    protected $table       = 'category';
    protected $modelCached = true;

    /**
     * 新增分类
     * @param array $data
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
     * @throws CategoryException
     */
    public static function addData(array $data)
    {
        try {
            return self::query()->create($data);
        } catch (\Exception $e) {
            throw new CategoryException(
                CategoryCodeEnum::ERR_INSERT_ERROR, '', $e
            );
        }
    }

    /**
     * 批量新增分类
     * @param array $data
     * @return bool
     * @throws CategoryException
     */
    public static function addDataBatch(array $data)
    {
        try {
            return self::query()->insert($data);
        } catch (\Exception $e) {
            throw new CategoryException(
                CategoryCodeEnum::ERR_INSERT_ERROR, '', $e
            );
        }
    }

    /**
     * 根据id查询分类详情
     * @param $categoryId
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public static function getCategoryInfoById($categoryId)
    {
        return CategoryModel::query()->where(['id' => $categoryId])->first();
    }

    /**
     * @param $categoryId
     * @param $data
     * @return int
     */
    public static function setCategoryInfoById($categoryId, $data): int
    {
        return CategoryModel::query()->where(['id' => $categoryId])->update($data);
    }

    /**
     * 根据id设置分类信息
     * @param $categoryId
     * @param $column
     * @param $value
     * @return int
     */
    public static function setCateGoryColumnById($categoryId, $column, $value): int
    {
        return CategoryModel::query()->where(['id' => $categoryId])->update([$column => $value]);
    }

    public static function getCateListByIds($cateIds)
    {
        return CategoryModel::query()->whereIn('id', $cateIds)->get();
    }

    /**
     * 根据brandId查询分类列表
     * 缓存方法，使用CategoryService::getCategoryList
     * @param int $brandId
     */
    public static function getListByBrandId(
        int $brandId,
        int $menuId,
        array $selectColumn = ['*'],
        int $needCategory = 0
    ) {
        $customPackageCategoryId = ConfigService::getPackageCategoryId();

        return CategoryModel::query()
                            ->where('brandId', $brandId)
                            ->where('menuId', $menuId)
                            ->when($needCategory, function ($query) use ($customPackageCategoryId) {
                                return $query->orWhere('id', $customPackageCategoryId);
                            })
                            ->orderBy("sort")
                            ->select($selectColumn)
                            ->get();
    }
}
